package com.manage.function.service.impl;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import com.manage.common.core.domain.entity.SysUser;
import com.manage.common.utils.DateUtils;
import com.manage.common.utils.SecurityUtils;
import com.manage.function.domain.vo.Constants;
import com.manage.system.domain.SysUserRole;
import com.manage.system.mapper.SysDeptMapper;
import com.manage.system.mapper.SysUserMapper;
import com.manage.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.manage.function.mapper.FunctionCheckInMapper;
import com.manage.function.domain.FunctionCheckIn;
import com.manage.function.service.IFunctionCheckInService;

import javax.annotation.Resource;

/**
 * 打卡记录Service业务层处理
 *
 * @author jinghj
 * @date 2025-03-14
 */
@Service
public class FunctionCheckInServiceImpl implements IFunctionCheckInService
{
    @Autowired
    private FunctionCheckInMapper functionCheckInMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询打卡记录
     *
     * @param id 打卡记录主键
     * @return 打卡记录
     */
    @Override
    public FunctionCheckIn selectFunctionCheckInById(Long id)
    {
        return functionCheckInMapper.selectFunctionCheckInById(id);
    }

    /**
     * 查询打卡记录列表
     *
     * @param functionCheckIn 打卡记录
     * @return 打卡记录
     */
    @Override
    public List<FunctionCheckIn> selectFunctionCheckInList(FunctionCheckIn functionCheckIn)
    {
        List<FunctionCheckIn> functionCheckIns=null;
        String username = SecurityUtils.getUsername();
        SysUser sysUser = sysUserMapper.selectUserByUserName(username);
        Long userId = sysUser.getUserId();
        SysUserRole sysUserRole = sysUserRoleMapper.selectSysUserRoleByUserId(userId);
        if (sysUserRole.getRoleId()==1){
            functionCheckIns=functionCheckInMapper.selectFunctionCheckInList(functionCheckIn);
        }else {
           functionCheckIn.setUserName(username);
           functionCheckIns=functionCheckInMapper.selectFunctionCheckInList(functionCheckIn);
        }

        return functionCheckIns;
    }

    /**
     * 新增打卡记录
     *
     * @param functionCheckIn 打卡记录
     * @return 结果
     */
    @Override
    public int insertFunctionCheckIn(FunctionCheckIn functionCheckIn)
    {
        String username = SecurityUtils.getUsername();
        SysUser sysUser = sysUserMapper.selectUserByUserName(username);
//获取上班当前打卡时间 格式yyyy-MM-dd HH:mm:ss
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(formatter1.format(now), formatter1);
        //早上未打卡
        if(functionCheckIn.getCheckInTime()==null){
            functionCheckIn.setUserId(sysUser.getUserId());
            functionCheckIn.setCheckInTime(null);
            functionCheckIn.setCheckStatus(null);
            functionCheckIn.setCheckEndTime(null);
            return functionCheckInMapper.insertFunctionCheckIn(functionCheckIn);
        }
        //打卡时间超过早上8点就是迟到
        if(functionCheckIn.getCheckInTime().getHour()>=8){
            functionCheckIn.setCheckInTime(parse);
            functionCheckIn.setCheckStatus(Constants.CHECK_IN_LATE_STATUS);
            functionCheckIn.setCheckEndTime(null);
            functionCheckIn.setUserId(sysUser.getUserId());
            return functionCheckInMapper.insertFunctionCheckIn(functionCheckIn);
        }
        //打卡时间小于早上8点就是正常
        if(functionCheckIn.getCheckInTime().getHour()<8){
            functionCheckIn.setCheckInTime(parse);
            functionCheckIn.setCheckStatus(null);
            functionCheckIn.setCheckEndTime(null);
            functionCheckIn.setUserId(sysUser.getUserId());
            return functionCheckInMapper.insertFunctionCheckIn(functionCheckIn);
        }
        return functionCheckInMapper.insertFunctionCheckIn(functionCheckIn);
    }

    /**
     * 修改打卡记录
     *
     * @param functionCheckIn 打卡记录
     * @return 结果
     */
    @Override
    public int updateFunctionCheckIn(FunctionCheckIn functionCheckIn)
    {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(formatter1.format(now), formatter1);
        //未打卡
        if(functionCheckIn.getCheckEndTime()==null || functionCheckIn.getCheckEndTime()==null){
            functionCheckIn.setCheckStatus(Constants.CHECK_IN_NOT_STATUS);
            return functionCheckInMapper.updateFunctionCheckIn(functionCheckIn);
        }
        //正常
        if(functionCheckIn.getCheckEndTime().getHour()>=18 && functionCheckIn.getCheckInTime().getHour()<8){
            functionCheckIn.setCheckStatus(Constants.CHECK_IN_LATE_STATUS);
            functionCheckIn.setCheckInTime(parse);
            return functionCheckInMapper.updateFunctionCheckIn(functionCheckIn);
        }
        //迟到
        if(functionCheckIn.getCheckInTime().getHour()>=8 && functionCheckIn.getCheckEndTime().getHour()>=18){
            functionCheckIn.setCheckEndTime(functionCheckIn.getCheckEndTime());
            return functionCheckInMapper.updateFunctionCheckIn(functionCheckIn);
        }
        //早退
        if(functionCheckIn.getCheckInTime().getHour()<8 && functionCheckIn.getCheckEndTime().getHour()<18){
            functionCheckIn.setCheckStatus(Constants.CHECK_IN_EARLY_STATUS);
            functionCheckIn.setCheckEndTime(parse);
            return functionCheckInMapper.updateFunctionCheckIn(functionCheckIn);
        }

        return functionCheckInMapper.updateFunctionCheckIn(functionCheckIn);
    }

    /**
     * 批量删除打卡记录
     *
     * @param ids 需要删除的打卡记录主键
     * @return 结果
     */
    @Override
    public int deleteFunctionCheckInByIds(Long[] ids)
    {
        return functionCheckInMapper.deleteFunctionCheckInByIds(ids);
    }

    /**
     * 删除打卡记录信息
     *
     * @param id 打卡记录主键
     * @return 结果
     */
    @Override
    public int deleteFunctionCheckInById(Long id)
    {
        return functionCheckInMapper.deleteFunctionCheckInById(id);
    }


}
